/*
+--------------------------------------------------------------------------
|   Mblog [#RELEASE_VERSION#]
|   ========================================
|   Copyright (c) 2014, 2015 mtons. All Rights Reserved
|   http://www.mtons.com
|
+---------------------------------------------------------------------------
*/
package mblog.web.controller.site;

import mblog.base.lang.Consts;
import mblog.modules.blog.data.PostVO;
import mblog.modules.blog.entity.Channel;
import mblog.modules.blog.service.ChannelService;
import mblog.modules.blog.service.PostService;
import mblog.web.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.Assert;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * Channel 主页
 * @author langhsu
 *
 */
@Controller
public class ChannelController extends BaseController {
	@Autowired
	private ChannelService channelService;
	@Autowired
	private PostService postService;
	
	  @RequestMapping({"/channel/{id}"})
	  public String channel(@PathVariable Integer id, ModelMap model, HttpServletRequest request)
	  {
	    String order = ServletRequestUtils.getStringParameter(request, "order", "newest");
	    int pn = ServletRequestUtils.getIntParameter(request, "pn", 1);
	    
	    Channel channel = this.channelService.getById(id.intValue());
	    String suffix = channel.getSuffix();
	    if ((suffix != null) && (!"".equals(suffix))) {
	      suffix = "_" + suffix;
	    } else {
	      suffix = "";
	    }
	    model.put("channel", channel);
	    model.put("order", order);
	    model.put("pn", Integer.valueOf(pn));
	    return view("/channel/index" + suffix);
	  }

	@RequestMapping("/view/{id}")
	public String view(@PathVariable Long id, ModelMap model) {
		PostVO view = postService.get(id);

		Assert.notNull(view, "该文章已被删除");

		postService.identityViews(id);
		model.put("view", view);
		return view(Views.ROUTE_POST_VIEW);
	}
}
